﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ;
using Zoomtel.Persist.BIZ.Models;
namespace Zoomtel.Service.BIZ
{
    public class ItemService
    {
        private ItemRepository _itemRepository;
        public ItemService(ItemRepository itemRepository)
        {
            _itemRepository = itemRepository;
        }


        //public object GetItemIdByItemCode(string itemCode)
        //{
        //  return  _itemRepository.GetItemIdByItemCode(itemCode);

        //}

        public  Task<ItemEntity> Get(string itemId)
        {
            var model= _itemRepository.FindEntityAsync(itemId);
            return model;
        }


        /// <summary>
        /// 查询Tag列表
        /// </summary>
        /// <returns></returns>
        public IResultModel QueryCat()
        {
            var result= _itemRepository.QueryCat();
            return ResultModel.Success(result);
        }

        /// <summary>
        /// 查询产品列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IResultModel> Query(ItemQueryModel model)
        {
            var result =await  _itemRepository.Query(model);

            return ResultModel.Success(result);
        }

        public async Task<IResultModel> Add(ItemEntity model)
        {
            var result = new ResultModel<int>();

            _itemRepository.BeginTrans();

            try {
                int i = 0;
                var platForms = model.PlatForm.Split(',');
                foreach (var platForm in platForms)
                {
                    i++;
                    model.PlatForm = platForm;
                    model.ItemCat = model.ItemCat.Trim();
                    model.ItemId = _itemRepository.GetSeq("S_BIZ_ITEM").ToString();

                    _itemRepository.Insert(model);
                    
                }
                
                if (i > 0)
                {
                    _itemRepository.Commit();
                    return result.Success(i);
                }
                else
                {
                    _itemRepository.Rollback();
                    return result.Failed();

                }
            }
            catch (Exception e) {
                _itemRepository.Rollback();
                return result.Failed(e.Message);
            }
            
        }

        public async Task<IResultModel> Update(ItemEntity model)
        {
            var result = new ResultModel<int>();

            int i = _itemRepository.Update(model);
            if (i > 0)
            {

                return result.Success(i);
            }
            else
            {
                return result.Failed();

            }

        }

        public async Task<IResultModel> delete(string itemId)
        {
            var result = new ResultModel<int>();
            
            int i = await _itemRepository.delete(itemId);
            if (i > 0)
            {

                return result.Success(i);
            }
            else
            {
                return result.Failed();

            }

        }

    }
}
